package vue;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import manager.LoadVariousList;
import action.ActionChangerLangue;
import action.ActionConsulterParties;
import action.ActionDeconnecter;
import action.ActionNouvellePartie;
import action.ActionPlay;
import action.ActionQuitter;
import bean.CombinaisonBean;
import bean.PlayMessage;

public class FenetrePrincipale extends JFrame implements KeyListener, GenericForm{

	private CombinaisonBean bonneCombinaison;
	private PanelCombinaison combi;
	private PanelChoixMenu choiceMenu;
	private PanelJeu game;
	private PanelVerifJeu verif;
	private PanelTitre title;
	private Pion[] soluce = new Pion[4]; 

	private JMenu menuLangues, menuFichier, menuAide;
	private JMenuItem itemMAnglais, itemMFrancais, itemNouveau, itemConsult, itemDeco, itemQuitter, itemCommandes;

	private Locale locale;
	private ResourceBundle bundle;

	private FenetrePrincipale currentForm;

	public FenetrePrincipale(){
		super();
		currentForm = this;
		locale = Locale.getDefault();
		bundle = ResourceBundle.getBundle("ressources", locale);
		build();
		this.setLangue(bundle);
	}

	public void build(){
		System.out.println("test : " + LoadVariousList.getIdUser());
		this.setSize(460,640);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setJMenuBar(buildMenuBar());
		setContentPane(buildContentPane());
		this.addKeyListener(this);
		this.setVisible(true);
		this.requestFocusInWindow();
	}

	public JPanel buildContentPane()
	{
		JPanel panel = new JPanel();

		panel.setLayout(new BorderLayout());

		JPanel panelMiddle = new JPanel();
		panelMiddle.setLayout(new BorderLayout());

		title = new PanelTitre(this.bundle.getString("messageTitle"));
		panel.add(title, BorderLayout.NORTH);

		game = new PanelJeu();
		panelMiddle.add(game,BorderLayout.CENTER);

		combi = new PanelCombinaison();
		panelMiddle.add(combi, BorderLayout.SOUTH);

		panel.add(panelMiddle, BorderLayout.CENTER);

		choiceMenu = new PanelChoixMenu();
		panel.add(choiceMenu, BorderLayout.WEST);

		verif = new PanelVerifJeu();
		panel.add(verif, BorderLayout.EAST);

		return panel;
	}

	public JMenuBar buildMenuBar(){
		JMenuBar menuPrincipal = new JMenuBar();

		menuFichier = new JMenu();
		itemNouveau = new JMenuItem(new ActionNouvellePartie(this.bundle.getString("itemNouveau"),currentForm));
		itemConsult = new JMenuItem(new ActionConsulterParties(this.bundle.getString("itemConsult"),currentForm));
		itemDeco = new JMenuItem(new ActionDeconnecter(this.bundle.getString("itemDeco"),currentForm));
		itemQuitter = new JMenuItem(new ActionQuitter(this.bundle.getString("itemQuitter")));
		menuFichier.add(itemNouveau);
		menuFichier.add(itemConsult);
		menuFichier.add(itemDeco);
		menuFichier.add(itemQuitter);
		menuPrincipal.add(menuFichier);

		menuLangues = new JMenu();
		itemMAnglais = new JMenuItem(new ActionChangerLangue(this,this.bundle.getString("itemMAnglais")));
		itemMFrancais = new JMenuItem(new ActionChangerLangue(this,this.bundle.getString("itemMFrancais")));
		menuLangues.add(itemMAnglais);
		menuLangues.add(itemMFrancais);
		menuPrincipal.add(menuLangues);

		menuAide = new JMenu(this.bundle.getString("menuAide"));
		itemCommandes = new JMenuItem(new ActionAfficherCommandes(this,this.bundle.getString("itemCommandes")));
		menuAide.add(itemCommandes);
		menuPrincipal.add(menuAide);

		return menuPrincipal;	
	}

	@Override
	public void keyPressed(KeyEvent e) {

		// TODO Auto-generated method stub
		int KeyCode = e.getKeyCode();
		if (KeyCode == KeyEvent.VK_UP){
			this.combi.changerCouleurUp();
		}
		else if (KeyCode == KeyEvent.VK_DOWN){
			this.combi.changerCouleurDown();
		}
		else if (KeyCode == KeyEvent.VK_RIGHT){
			this.combi.changerPositionRight();
		}
		else if (KeyCode == KeyEvent.VK_LEFT){
			this.combi.changerPositionLeft();
		}
		else if (KeyCode == KeyEvent.VK_ENTER){
			if (this.combi.estValide())
			{
				game.setPions(choiceMenu.getPositionFleche(), combi.getPions());

				ActionPlay action = new ActionPlay();
				PlayMessage pm = action.doPlay(combi.getEssaiCombi());

				verif.placerPetitsPions(choiceMenu.getPositionFleche(), pm.getNbNoirs(),pm.getNbBlancs());

				if (pm.isPerdu())
				{
					JOptionPane.showMessageDialog(this, this.bundle.getString("messagePerdre"),this.bundle.getString("messagePerdre"), JOptionPane.WARNING_MESSAGE);
					this.itemNouveau.doClick();
				}
				else if (pm.getNbNoirs() == 4)
				{
					JOptionPane.showMessageDialog(this, this.bundle.getString("messageGagner"),this.bundle.getString("messageGagner"), JOptionPane.WARNING_MESSAGE);
					this.itemNouveau.doClick();
				}
				else
				{
					choiceMenu.monterFleche();
				}
				combi.reinit();
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	public ResourceBundle getBundle(){
		return this.bundle;
	}

	public Locale getLocale(){
		return this.locale;
	}

	public void setBundle(ResourceBundle bundle){
		this.bundle = bundle;
	}

	@Override
	public void setLangue(ResourceBundle langue) {
		this.setTitle(langue.getString("TitleFenetrePrinc"));
		this.menuLangues.setText(langue.getString("menuLangues"));
		this.itemMAnglais.setText(langue.getString("itemMAnglais"));
		this.itemMFrancais.setText(langue.getString("itemMFrancais"));
		this.menuFichier.setText(langue.getString("menuFichier"));
		this.itemNouveau.setText(langue.getString("itemNouveau"));
		this.itemConsult.setText(langue.getString("itemConsult"));
		this.itemDeco.setText(langue.getString("itemDeco"));
		this.itemQuitter.setText(langue.getString("itemQuitter"));
		this.menuAide.setText(langue.getString("menuAide"));
		this.itemCommandes.setText(langue.getString("itemCommandes"));
		this.title.setTitre(langue.getString("messageTitle"));
	}

	public class ActionAfficherCommandes extends AbstractAction
	{
		private String titre;
		private FenetrePrincipale fenetre;

		public ActionAfficherCommandes(FenetrePrincipale fenetre, String t)
		{
			super(t);
			this.titre = t;
			this.fenetre = fenetre;
		}

		public void actionPerformed(ActionEvent arg0) {
			JOptionPane.showMessageDialog(fenetre, fenetre.bundle.getString("aide"),fenetre.bundle.getString("itemCommandes"), JOptionPane.QUESTION_MESSAGE);

		}
	}
}
